package com.yue.design.prototype;
// 实现接口cloneable

import java.io.*;

public class Student implements Cloneable, Serializable {
    private String name;
    private int age;
    private School school;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    @Override
    public Student clone() throws CloneNotSupportedException {
        //1 父类的clone只是浅克隆，引用数据类型指向一致
        //return (Student)super.clone();

        // 2 深度克隆的话需要序列化并发序列化

        Student clone=null;
        try{
            //序列化
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

            objectOutputStream.writeObject(this);
            //反序列化
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            clone = (Student)objectInputStream.readObject();

        }catch (Exception e){}
        return clone;

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}
